﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace MenuGame2DXNA
{
    
    public class TextEntity : VisibleGameEntity
    {
        private String nameEntity;

        public String NameEntity
        {
            get { return nameEntity; }
            set { nameEntity = value; }
        }

        
        private SpriteFont textFont;

        private String text;

        private bool isNumber;

        private float scale;

        private Color color;

    

        private int Time;

        private int NumberDest;

        private float rotation;

        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        private String formatString;

        public String FormatString
        {
            get { return formatString; }
            set { formatString = value; }
        }

       
        private Alignment align;

        public Alignment Align
        {
            get { return align; }
            set { align = value; }
        }


        public Color ColorText
        {
            get { return color; }
            set { color = value; }
        }
        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        public bool IsNumber
        {
            get { return isNumber; }
            set { isNumber = value; }
        }
        public String Text
        {
            get { return text; }
            set { text = value; }
        }
        public SpriteFont TextFont
        {
            get { return textFont; }
            set { textFont = value; }
        }
        public TextEntity()
        {

        }
        public TextEntity(ContentManager contenManeger, String NameFont,
            Vector2 _size,
            Vector2  _position,
             String _text = "",
            Alignment _align =  Alignment.Left,
            float _scale = 1
            )
        {
            TextFont = contenManeger.Load<SpriteFont>(NameFont);
            TopLeft = new Vector2(0,0);
            PositionStart = _position;
            text = _text;
            isNumber = false;
            Align = _align;
            Time = 0;
            Scale = _scale;        
            NumberDest = 0;
            rotation = 0;
            color = Color.White;
        }
        public TextEntity(ContentManager contenManeger,string _text="")
        {
            TextFont = contenManeger.Load<SpriteFont>("fontNormal");         
            text = _text;
            isNumber = false;
            Align = Alignment.Left;
            Time = 0;
            Scale = 1;
            TopLeft = new Vector2(0, 0);
            NumberDest = 0;
            rotation = 0;
            color = Color.White;
            Size = new Vector2(150, 25);
            PositionStart = new Vector2(0, 0);
        }

        public void setNumber(int Number,String format, int _Time = 1)
        {
            if (Number != NumberDest)
            {
                Time = _Time;
                IsNumber = true;
                NumberDest = Number;
                FormatString = format;
            }
            
        }
        int tick = 0;
        public override void Update(GameTime gameTime, Color color)
        {
            if (isNumber)
            {
                int DisTime = Time - tick;
                int DisNum = NumberDest - int.Parse(text);
                int incrice = DisNum / DisTime;
                text = (int.Parse(text) + incrice).ToString(FormatString);
                tick++;
                if(Time <= tick)
                {
                    IsNumber = false;
                    text = NumberDest.ToString(FormatString);
                    tick = 0;
                }
            }
            else
                tick = 0;
        }

        
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Rectangle bounds = new Rectangle((int)(PositionStart.X + TopLeft.X),
                (int)(PositionStart.Y + TopLeft.Y),
                    (int)Size.X, (int)Size.Y);
            Vector2 size = textFont.MeasureString(text);
            Vector2 pos = new Vector2(bounds.Center.X, bounds.Center.Y);
            Vector2 origin = size * 0.5f;

            if (align.HasFlag(Alignment.Left))
                origin.X += bounds.Width / 2 - size.X / 2;

            if (align.HasFlag(Alignment.Right))
                origin.X -= bounds.Width / 2 - size.X / 2;

            if (align.HasFlag(Alignment.Top))
                origin.Y += bounds.Height / 2 - size.Y / 2;

            if (align.HasFlag(Alignment.Bottom))
                origin.Y -= bounds.Height / 2 - size.Y / 2;

            spriteBatch.DrawString(textFont, text, pos, color, Rotation, origin, Scale, SpriteEffects.None, 0);
            
        }
        [Flags]
        public enum Alignment { Center = 0, Left = 1, Right = 2, Top = 4, Bottom = 8 }

        
    }
}
